const os = require('os')
const platform = os.platform()
const release = os.release()

const { contextBridge } = require('electron')
const path = require('path')
const fs = require('fs')
// preload.js早于index.html解析之前执行，如果需要操作dom,需要监听DOMContentLoaded
document.addEventListener('DOMContentLoaded',()=>{
    document.getElementById('platform').append(platform)
    document.getElementById('release').append(release)
})

// 从预加载脚本将api暴露给渲染器
contextBridge.exposeInMainWorld('electron',{
    doSomething:()=>{ return console.log('something')},
    saveFile: (filepath,content) => {
        fs.writeFile(path.join(__dirname,filepath),content,error=>{
            console.log(error, "error")
        })
    },
    downloadFile:(filepath,content)=>{
        const blob = new Blob([content],{type: 'text/plain;charset=utf-8'})
        // 根据blob 生产url 链接
        const url = new URL.createObjectURL(blob)
        // 创建a标签下载
        const a = documnet.createElement('a')
        a.href = url
        a.download = filepath
        a.click()
        URL.revokeObjectURL(url)
    }
})

